<?php
namespace app\index\controller;
class Test
{
    /**
     * 中国民生银行请求测试
     */
    public function getRequest(){
        $xmldata = '<?xml version="1.0" encoding="GBK"?>
<CMBC header="100" version="100" security="none" lang="chs" trnCode="Xfer">
<requestHeader>
<dtClient>2018-10-08 15:57:44</dtClient>
<clientId>2200003220</clientId>
<userId>2200003220001</userId>
<userPswd>******</userPswd>
<language>chs</language>
<appId>nsbdes</appId>
<appVer>201</appVer>
</requestHeader>
<xDataBody>
		<trnId>84412015</trnId>

	</xDataBody>
</CMBC>
</CMBC>
';
        $url = 'http://192.168.1.164:8080/eweb/b2e/connect.do';
        $response = $this -> curl_post_ssl($url,$xmldata);
        $str = iconv("gbk", "utf-8//ignore", $response);
        $info = $this -> xml2array($response);
        dump($info);
        dump($str);
    }

    public function inquire(){
        $xmldata = '<?xml version="1.0" encoding="gb2312"?>
<CMBC header="100" version="100" security="none" lang="chs" trnCode="qryXfer">
	<requestHeader>
		<dtClient>2008-03-20 10:05:33</dtClient>
		<clientId>2200003220</clientId>
		<userId>2200003220001</userId>
		<userPswd>111111</userPswd>
		<language>chs</language>
		<appId>nsbdes</appId>
		<appVer>201</appVer>
	</requestHeader>
	<xDataBody>

	</xDataBody>
</CMBC>
';
        $url = 'http://192.168.1.164:8080/eweb/b2e/connect.do';
        $response = $this -> curl_post_ssl($url,$xmldata);
        $str = iconv("gbk", "utf-8//ignore", $response);
        $info = $this -> xml2array($response);
        dump($info);
        dump($str);
    }

    public function ceshi(){
        echo '111';exit;
        $xmldata = '<?xml version="1.0" encoding="gb2312"?>
<CMBC header="100" version="100" security="none" lang="chs" trnCode="QryAcctStatus">
	<requestHeader>
		<dtClient>2008-03-20 10:05:33</dtClient>
		<clientId>2200003220</clientId>
		<userId>2200003220001</userId>
		<userPswd>111111</userPswd>
		<language>chs</language>
		<appId>nsbdes</appId>
		<appVer>201</appVer>
	</requestHeader>
	<xDataBody>
		<trnId>n3201d08</trnId>
		<acntToNo>600150299</acntToNo>
		<acntToName>4001机构流动利测试数据</acntToName>
		<acntNo>600033053</acntNo>
	</xDataBody>
</CMBC>
';
        $url = 'http://192.168.1.164:8080/eweb/b2e/connect.do';
        $response = $this -> curl_post_ssl($url,$xmldata);
        $str = iconv("gbk", "utf-8//ignore", $response);
        $info = $this -> xml2array($response);
        dump($info);
        dump($str);
    }

    /**
     * 将xml转为array
     * @param  string 	$xml xml字符串
     * @return array    转换得到的数组
     */
    protected function xml2array($xml){
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        $result= json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $result;
    }

    protected function curl_post_ssl($url,$xmldata){
        $hearder = array('Content-type:text/xml');
        $ch = curl_init(); //初始化curl
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,$url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $hearder);

        curl_setopt($ch,CURLOPT_POST, 1);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$xmldata);
        $response = curl_exec($ch);

        if (curl_errno($ch)){
            print curl_error($ch);
        }
        curl_close($ch);
        return $response;
    }
}
